package com.slicktechnologies.client.views.contract;
import java.util.List;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ButtonBase;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.simplesoftwares.client.library.FieldType;
import com.simplesoftwares.client.library.FlexForm.FormStyle;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.FormFieldBuilder;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.appskeleton.ScreeenState;
import com.simplesoftwares.client.library.appstructure.formscreen.FormScreen;
import com.simplesoftwares.client.library.composite.PaymentComposite;
import com.simplesoftwares.client.library.composite.PersonInfoComposite;
import com.simplesoftwares.client.library.composite.UploadComposite;
import com.simplesoftwares.client.library.mywidgets.ObjectListBox;
import com.slicktechnologies.client.utility.AppConstants;
import com.slicktechnologies.client.utility.AppUtility;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.shared.Contract;
import com.slicktechnologies.shared.SalesLineItem;
import com.slicktechnologies.shared.ServiceType;
import com.slicktechnologies.shared.common.RecievableGoods;
import com.slicktechnologies.shared.common.businessprocesslayer.Customer;
import com.slicktechnologies.shared.common.businessunitlayer.Branch;
import com.slicktechnologies.shared.common.helperlayer.Config;
import com.slicktechnologies.shared.common.paymentlayer.Payment;
import com.slicktechnologies.shared.common.personlayer.Employee;


public class ContractForm extends FormScreen<Contract> implements ClickHandler,ChangeHandler {

	//***********************************Variable Declaration Starts********************************************//
	//Token to add the varialble declarations
	public PaymentComposite paymentComposite;
	
	ObjectListBox<Branch> olbbBranch;
	ObjectListBox<Employee> olbeSalesPerson;
	PaymentTable paymenttable;
	IntegerBox tbContractId;
	TextBox tbQuotationStatus;
	TextArea taDescription;
	UploadComposite ucUploadTAndCs;
	PersonInfoComposite personInfoComposite;
	ProductComposite productComposite;
	Button btnAddPayment;
	SalesLineItemTable saleslineitemtable;
	ObjectListBox<ServiceType>serviceType;
	CustomerParticularTable customerParticularTable;
	ObjectListBox<Config>olbCustomerParticular;
	TextBox customerParticulars;
	DoubleBox diagPrice;
	Button addCustomerParticulars;
	//***********************************Variable Declaration Ends********************************************//


	public  ContractForm() {
		super();
		createGui();
		this.tbContractId.setEnabled(false);
		this.tbQuotationStatus.setEnabled(false);
		this.tbQuotationStatus.setText(Contract.CREATED);
		
		this.productComposite.getAddButton().addClickHandler(this);
		serviceType.addChangeHandler(this);
		
		
	}

	public ContractForm  (String[] processlevel, FormField[][] fields,
			FormStyle formstyle) 
	{
		super(processlevel, fields, formstyle);
		createGui();
	}

	//***********************************Variable Initialization********************************************//

	/**
	 * Method template to initialize the declared variables.
	 */
	@SuppressWarnings("unused")
	private void initalizeWidget()
	{

		paymentComposite=new PaymentComposite();
		olbbBranch=new ObjectListBox<Branch>();
		AppUtility.makeBranchListBoxLive(olbbBranch);
		paymenttable=new PaymentTable();
		paymenttable.connectToLocal();
		tbContractId=new IntegerBox();
		tbQuotationStatus=new TextBox();
		taDescription=new TextArea();
		ucUploadTAndCs=new UploadComposite();
		saleslineitemtable=new SalesLineItemTable();
		personInfoComposite=AppUtility.customerInfoComposite(new Customer());
		olbeSalesPerson=new ObjectListBox<Employee>();
		AppUtility.makeSalesPersonListBoxLive(olbeSalesPerson);
		btnAddPayment=new Button("Add Payment");
		btnAddPayment.addClickHandler(this);
		this.tbContractId.setEnabled(false);
		personInfoComposite.getCustomerId().getHeaderLabel().setText("* Customer ID");
		personInfoComposite.getCustomerName().getHeaderLabel().setText("* Customer Name");
		personInfoComposite.getCustomerCell().getHeaderLabel().setText("* Customer Cell");
		productComposite=new ProductComposite();
		serviceType=new ObjectListBox<ServiceType>();
		ServiceType.makeServiceTypeListBoxLive(serviceType);
		diagPrice=new DoubleBox();
		customerParticularTable=new CustomerParticularTable();
		olbCustomerParticular=new ObjectListBox<Config>();
		AppUtility.MakeLiveConfig(olbCustomerParticular,Screen.CUSTOMERPARTICULARS);
		customerParticulars=new TextBox();
		olbCustomerParticular.addChangeHandler(this);
		addCustomerParticulars=new Button("Add");
		addCustomerParticulars.addClickHandler(this);
	}

	/**
	 * method template to create screen formfields
	 */
	@Override
	public void createScreen() {


		initalizeWidget();
		

		//Token to initialize the processlevel menus.
		this.processlevelBarNames=new String[]{AppConstants.DIAGOSED,AppConstants.Unsucessful,
				AppConstants.CLOSE,AppConstants.Sucessful,AppConstants.NEW,};
		
		//////////////////////////// Form Field Initialization////////////////////////////////////////////////////
		//Token to initialize formfield
		FormFieldBuilder fbuilder;
		fbuilder = new FormFieldBuilder();
		FormField fgroupingCustomerInformation=fbuilder.setlabel("Customer Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("",personInfoComposite);
		FormField fpersonInfoComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		
		
		
		fbuilder = new FormFieldBuilder("Contract Id",tbContractId);
		FormField ftbContractId= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		
		
		
		
		
		
		fbuilder = new FormFieldBuilder();
		FormField fgroupingDocuments=fbuilder.setlabel("Documents").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("Upload T&Cs",ucUploadTAndCs);
		FormField fucUploadTAndCs= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder();
		FormField fgroupingSalesInformation=fbuilder.setlabel("Sales Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("Status",tbQuotationStatus);
		FormField ftbQuotationStatus= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("* Service Type",serviceType);
		FormField fserviceType= fbuilder.setMandatory(true).setRowSpan(0).setColSpan(0).setMandatoryMsg("Service Type is Mandatory").
				build();
		
		fbuilder = new FormFieldBuilder("Diagnosis Price",diagPrice);
		FormField fdiagPrice= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		
		
		fbuilder = new FormFieldBuilder("* Branch",olbbBranch);
		FormField folbbBranch= fbuilder.setMandatory(true).setMandatoryMsg("Branch is Mandatory").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Sales Person",olbeSalesPerson);
		FormField folbeSalesPerson= fbuilder.setMandatory(true).setMandatoryMsg("Sales Person is Mandatory").setRowSpan(0).setColSpan(0).build();
		
		
		
		
		
		fbuilder = new FormFieldBuilder();
		FormField fgroupingDescription=fbuilder.setlabel("Description").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("Description",taDescription);
		FormField ftaDescription= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		fbuilder = new FormFieldBuilder();
		
		
		
		FormField fotmComposite= fbuilder.setMandatory(true).setMandatoryMsg("Mandatory").setRowSpan(0).setColSpan(4).build();
		fbuilder = new FormFieldBuilder();
		FormField fgroupingAddPayment=fbuilder.setlabel("Add Payment").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("",paymentComposite);
		FormField fpaymentComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		fbuilder = new FormFieldBuilder();
		FormField fgroupingPaymenthistory=fbuilder.setlabel("Payment History").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("",paymenttable.getTable());
		FormField fpaymenttable= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();

		fbuilder = new FormFieldBuilder();
		FormField frecievedMaterialForRepair=fbuilder.setlabel("Material Recieved For Repair").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		
		fbuilder = new FormFieldBuilder("",saleslineitemtable.getTable());
		FormField fsaleslineitemtable= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		
		fbuilder = new FormFieldBuilder("",productComposite);
		FormField fproductComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		
		fbuilder = new FormFieldBuilder("",customerParticularTable.getTable());
		FormField fcustParticularTable= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		
		fbuilder = new FormFieldBuilder("Customer Items",olbCustomerParticular);
		FormField folbParticulars= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("Other",customerParticulars);
		FormField ftbParticulars= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("",addCustomerParticulars);
		FormField fbtnaddparticulars= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		
		FormField fgroupingCustomerParticulars=fbuilder.setlabel("Customer Items").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		
		
		
		
		fbuilder = new FormFieldBuilder("",btnAddPayment);
		FormField fbtnAdd= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();

		
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		////////////////////////////////Lay Out Making Code///////////////////////////////////////////////////////////////


		FormField[][] formfield = {   {fgroupingCustomerInformation},
				{fpersonInfoComposite},
				{ftbContractId},
				
				{fgroupingDocuments},
				{fucUploadTAndCs},
				{fgroupingSalesInformation},
				{ftbQuotationStatus,folbbBranch,folbeSalesPerson,fserviceType},
				{fdiagPrice},
				
			
				{fgroupingDescription},
				{ftaDescription},
				{fgroupingCustomerParticulars},
				{folbParticulars,ftbParticulars,fbtnaddparticulars},
				{fcustParticularTable},
				{frecievedMaterialForRepair},
				{fproductComposite},
				{fsaleslineitemtable},
				
				{fpaymentComposite},
				{fotmComposite},
				{fgroupingAddPayment},
				{fpaymentComposite},
				{fbtnAdd},
				{fgroupingPaymenthistory},
				{fpaymenttable},
		};

		this.fields=formfield;

	}

	/**
	 * method template to update the model with token entity name
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void updateModel(Contract model) 
	{
		if(personInfoComposite.getValue()!=null)
			model.setCinfo(personInfoComposite.getValue());
		
		
		//if(validuntil.getValue()!=null)
			//model.setValidUntill(validuntil.getValue());
		   
			model.setDocument(ucUploadTAndCs.getValue());
		if(tbQuotationStatus.getValue()!=null)
			model.setStatus(tbQuotationStatus.getValue());
		if(olbbBranch.getValue()!=null)
			model.setBranch(olbbBranch.getValue());
		if(olbeSalesPerson.getValue()!=null)
			model.setEmployee(olbeSalesPerson.getValue());
		
		
		if(taDescription.getValue()!=null)
			model.setDescription(taDescription.getValue());
		
		if(paymenttable.getValue()!=null)
			model.setPayments(paymenttable.getValue());
		
		
		if(paymentComposite.totalAmt.getValue()!=null)
			model.setTotalAmount(paymentComposite.totalAmt.getValue());
		if(paymentComposite.netPayable.getValue()!=null)
			model.setNetpayable(paymentComposite.netPayable.getValue());
		if(paymentComposite.flatdiscount.getValue()!=null)
			model.setFlatDiscount(paymentComposite.flatdiscount.getValue());
		if(paymentComposite.percentageDiscount.getValue()!=null)
			model.setPerDiscount(paymentComposite.percentageDiscount.getValue());
		if(paymentComposite.getBalance().getValue()!=null)
			model.setbalance(paymentComposite.balance.getValue());
		if(paymentComposite.getTotalAmt()!=null)
			model.setGrandTotal(paymentComposite.getTotalAmt().getValue());
		if(diagPrice.getValue()!=null)
			model.setDiagnosisAmount(diagPrice.getValue());
		if(serviceType.getValue()!=null)
			model.setServiceType(serviceType.getValue());
	
		
		if(this.tbContractId.getValue()!=null)
			model.setContractCount(tbContractId.getValue());
		
		
		if(this.saleslineitemtable.getValue()!=null)
			model.setItems(saleslineitemtable.getValue());
		if(this.customerParticularTable.getValue()!=null)
			model.setCustomerItems(customerParticularTable.getValue());

		presenter.setModel(model);

	}

	/**
	 * method template to update the view with token entity name
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void updateView(Contract view) 
	{

        
        	tbContractId.setValue(view.getCount());
		if(view.getCinfo()!=null)
			personInfoComposite.setValue(view.getCinfo());
		
		if(view.getItems()!=null)
		  saleslineitemtable.setValue(view.getItems());
		
		
		if(view.getDocument()!=null)
			ucUploadTAndCs.setValue(view.getDocument());
		if(view.getStatus()!=null)
			tbQuotationStatus.setValue(view.getStatus());
		if(view.getBranch()!=null)
			olbbBranch.setValue(view.getBranch());
		if(view.getEmployee()!=null)
			olbeSalesPerson.setValue(view.getEmployee());
		
		
		if(view.getDescription()!=null)
			taDescription.setValue(view.getDescription());
		
		if(view.getPayments()!=null)
			paymenttable.setValue(view.getPayments());
		

			paymentComposite.netPayable.setValue(view.getNetpayable());
		
			
		
			paymentComposite.flatdiscount.setValue(view.getFlatDiscount());
		
			paymentComposite.totalAmt.setValue(view.getTotalAmount());
		
			paymentComposite.percentageDiscount.setValue(view.getPerDiscount());
		if(view.getbalance()!=0)
			paymentComposite.balance.setValue(view.getbalance());
		
		
		if(view.getServiceType()!=null)
			serviceType.setValue(view.getServiceType());
		if(view.getDiagnosisAmount()!=0)
			this.diagPrice.setValue(view.getDiagnosisAmount());
		
       if(view.getCustomerItems()!=null)
    	   this.customerParticularTable.setValue(view.getCustomerItems());
        
		presenter.setModel(view);

	}

	// Hand written code shift in presenter


	/**
	 * Toggles the app header bar menus as per screen state
	 */

	public void toggleAppHeaderBarMenu() {
		if(AppMemory.getAppMemory().currentState==ScreeenState.NEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Save")||text.contains("Discard")||text.contains("Search"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   
				
			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.EDIT)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Save")||text.contains("Discard"))
					menus[k].setVisible(true); 
				
				else
					menus[k].setVisible(false);  		   
			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.VIEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Edit")||text.contains("Discard")||text.contains("Search")||text.contains("Email")
						||text.contains("Print"))
						menus[k].setVisible(true); 
				
				else
					menus[k].setVisible(false);  		   
			}
		}
		
	}

/**
 * Toogels the Application Process Level Menu as Per Application State
 */
	public void toggleProcessLevelMenu()
	{
		Contract entity=(Contract) presenter.getModel();
		
		String status=entity.getStatus();

		for(int i=0;i<getProcesslevelBarNames().length;i++)
		{
			InlineLabel label=getProcessLevelBar().btnLabels[i];
			String text=label.getText().trim();

			
			
		
			
			if(status.equals(Contract.CLOSED)||status.equals(Contract.SUCESSFUL)||
					status.equals(Contract.UNSUCESSFUL))
			{

				if(text.contains(AppConstants.NEW))
					label.setVisible(true);
				else
					label.setVisible(false);
			}
			
			if(status.equals(Contract.DIAGNOSED))
			{

				if(text.contains(AppConstants.NEW)||text.contains(AppConstants.DIAGOSED))
					label.setVisible(true);
				else
					label.setVisible(false);
			}



		}

	}
	/**
	 * Sets the Application Header Bar as Per Status
	 */

	public void setAppHeaderBarAsPerStatus()
	{
		Contract entity=(Contract) presenter.getModel();
		String status=entity.getStatus();
		
		if(status.equals(Contract.CLOSED)||status.equals(Contract.UNSUCESSFUL)||status.equals(Contract.SUCESSFUL))
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Discard")||text.contains("Search")
						||text.contains("Print"))
				{
					menus[k].setVisible(true); 
					
				}
				else
				{
					menus[k].setVisible(false);  
					

				}

			}

		}

		else if(status.equals(Contract.APPROVED))
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Discard")||text.contains("Search")||text.contains("Email")
						||text.contains("Print")||text.contains("Save"))
				{
					menus[k].setVisible(true); 

				}
				else
				{
					menus[k].setVisible(false);  
			

				}

			}

		}


		
		

	}
	/**
	 * The method is responsible for changing Application state as per 
	 * status
	 */
	public void setMenuAsPerStatus()
	{
		this.setAppHeaderBarAsPerStatus();
		this.toggleProcessLevelMenu();
		
	}
	
	
	
	
	

	

	
	/**
	 * sets the id textbox with the passed count value. 
	 */
	@Override
	public void setCount(int count)
	{
		tbContractId.setValue(count);
	}


	@Override
	public void onClick(ClickEvent event) 
	{
		if(event.getSource().equals(btnAddPayment))
		{
			
			double cash = 0,cheque = 0,card = 0;
			if(paymentComposite.cashamt.getValue()!=null)
				cash=paymentComposite.cashamt.getValue();
			if(paymentComposite.cheqamt.getValue()!=null)
				cheque=paymentComposite.cheqamt.getValue();
			if(paymentComposite.credidamt.getValue()!=null)
				card=paymentComposite.credidamt.getValue();
			if(cash==0 && card==0 && cheque==0)
				;
			else
				reactOnAddPayment();
		}
		if(event.getSource().equals(productComposite.addButton))
		{
			//AppUtility.ReactOnAddForOneToManyComposite(otmComposite);
			if(productComposite.getBrand().getValue()!=null&&productComposite.getCategory().getValue()!=null)
			{
			SalesLineItem item=new SalesLineItem();
			item.setBrand(productComposite.getBrand().getValue());
			item.setCat(productComposite.getCategory().getValue());
			saleslineitemtable.getDataprovider().getList().add(item);
			
			calculateBalance();
			}
		}
		
		if(event.getSource().equals(addCustomerParticulars))
		{
			RecievableGoods goods=new RecievableGoods();
			String items;
			if(olbCustomerParticular.getValue()!=null)
			{
				items=olbCustomerParticular.getValue();
				goods.setName(items);
				
				customerParticularTable.getListDataProvider().getList().add(goods);
			}
			else
			{
				if((customerParticulars.getText().trim().equals("")==false)) 
				{
					goods.setName(customerParticulars.getText());
					customerParticularTable.getListDataProvider().getList().add(goods);
				}
			}
		}
		

	}

	public PaymentComposite getPaymentComposite() {
		return paymentComposite;
	}

	public void setPaymentComposite(PaymentComposite paymentComposite) {
		this.paymentComposite = paymentComposite;
	}
/*
	public DateBox getValiduntil() {
		return validuntil;
	}

	public void setValiduntil(DateBox validuntil) {
		this.validuntil = validuntil;
	}
*/
	public ObjectListBox<Branch> getOlbbBranch() {
		return olbbBranch;
	}

	public void setOlbbBranch(ObjectListBox<Branch> olbbBranch) {
		this.olbbBranch = olbbBranch;
	}

	

	public TextBox getTbQuotationStatus() {
		return tbQuotationStatus;
	}

	public void setTbQuotationStatus(TextBox tbQuotationStatus) {
		this.tbQuotationStatus = tbQuotationStatus;
	}

	public TextArea getTaDescription() {
		return taDescription;
	}

	public void setTaDescription(TextArea taDescription) {
		this.taDescription = taDescription;
	}

	public UploadComposite getUcUploadTAndCs() {
		return ucUploadTAndCs;
	}

	public void setUcUploadTAndCs(UploadComposite ucUploadTAndCs) {
		this.ucUploadTAndCs = ucUploadTAndCs;
	}

	

	public PersonInfoComposite getPersonInfoComposite() {
		return personInfoComposite;
	}

	public void setPersonInfoComposite(PersonInfoComposite personInfoComposite) {
		this.personInfoComposite = personInfoComposite;
	}

	public ObjectListBox<Employee> getOlbeSalesPerson() {
		return olbeSalesPerson;
	}

	public void setOlbeSalesPerson(ObjectListBox<Employee> olbeSalesPerson) {
		this.olbeSalesPerson = olbeSalesPerson;
	}

	

	
	public void reactOnAddPayment()
	{
		System.out.println("Payment Composite "+paymentComposite.getValue());
		
		Payment payment=paymentComposite.getValue();
		
		List<Payment>list=paymenttable.getDataprovider().getList();
		if(list!=null)
		{
			list.add(payment);
			calculateBalance();
			
		}
		
		clearPaymentComposite();
		
	}
	
	public void clearPaymentComposite()
	{
		getPaymentComposite().getCredidamt().setText("");
		getPaymentComposite().getChequenumber().setText("");
		getPaymentComposite().getCashamt().setText("");
		getPaymentComposite().getCheqamt().setText("");
		getPaymentComposite().getBankname().setText("");
		getPaymentComposite().getFlatdiscount().setText("");
		getPaymentComposite().getBankbranch().setText("");
		getPaymentComposite().getChequeDate().setValue(null);
		getPaymentComposite().percentageDiscount.setText("");
	}

	public SalesLineItemTable getSaleslineitemtable() {
		return saleslineitemtable;
	}
	public void setSaleslineitemtable(SalesLineItemTable saleslineitemtable) {
		this.saleslineitemtable = saleslineitemtable;
	}

	@Override
	public void clear() {
		super.clear();
		getstatustextbox().setText(Contract.CREATED);
		paymenttable.clear();
		
		
		
	}

	private TextBox getstatustextbox() {
		// TODO Auto-generated method stub
		return this.tbQuotationStatus;
	}

	@Override
	public void setEnable(boolean state) {
		super.setEnable(state);
		getstatustextbox().setEnabled(false);
		this.tbContractId.setEnabled(false);
		saleslineitemtable.setEnable(state);
		if(olbCustomerParticular.getValue()==null);
		     customerParticulars.setEnabled(true);
		customerParticularTable.setEnable(state);
		
	}

	@Override
	public void setToViewState() {
	
		super.setToViewState();
		setMenuAsPerStatus();
	
	}
	@Override
	public void setToEditState() {
	
		super.setToEditState();
		setMenuAsPerStatus();
	
	}
	
	@Override
	 public boolean validate()
	    {
	       
	        if(saleslineitemtable.getDataprovider().getList().size() == 0)
	        {
	            showDialogMessage("Please Select at least one Product!");
	            return false;
	        }
	        boolean superValidate = super.validate();
	        if(superValidate==false)
	        {
	        	 return false;
	        }
	       
	        return true;
	    }


	public void calculateBalance()
	{
		double paidAmt=paymenttable.calculatePaidAmt();
		if(paymentComposite.getNetPayable().getValue()!=null)
		{
		   double balance=paymentComposite.getNetPayable().getValue()-paidAmt;
		paymentComposite.balance.setValue(balance);
		}
	}

	
	public Button getBtnAddPayment() {
		return btnAddPayment;
	}

	public void setBtnAddPayment(Button btnAddPayment) {
		this.btnAddPayment = btnAddPayment;
	}

	@Override
	public void onChange(ChangeEvent event) {
		if(event.getSource()==serviceType)
		{
		if(serviceType.getSelectedItem()==null)
			this.diagPrice.setText("");
		else
		{
			ServiceType type=this.serviceType.getSelectedItem();
			this.diagPrice.setValue(type.getDiagnosisAmount());
		}
		}
		
		if(event.getSource()==this.olbCustomerParticular)
		{
			if(olbCustomerParticular.getSelectedIndex()==0)
			{
				customerParticulars.setEnabled(true);
				customerParticulars.setText("");
				
			}
			else
			{
				customerParticulars.setEnabled(false);
				customerParticulars.setText("");
			}
		}
		
	}
	
	@Override
	public void applyStyle()
	{
		super.applyStyle();
		tbContractId.setWidth("300px");
		tbQuotationStatus.setWidth("300px");
		diagPrice.setWidth("300px");
	}
	
}
